<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Transfer
 *
 * @author Massri
 */
class Application_Model_Sms {

    //put your code here
    private $_db;

    function __construct() {
        $registry = Zend_Registry::getInstance();
        $this->_db = $registry->database;
    }

    /*
     * $member = array(
     * member_code,member_name,pin,sponsor_id, member_type_id
     * )
     */

    function sendSMS($accNum, $tlp, $pesan, $stts) {

        $this->_db->beginTransaction();

        $ret = array();

        try {

            /*
             * Insert msg outbox
             */
            $dataInput = array($accNum, $tlp, $pesan, $stts);
            $stmt = $this->_db->query("CALL CreateReply(?, ?, ?, ?)", $dataInput);

            //$stmt = $this->_db->query("SELECT @out_code as out_code, @out_msg as out_msg");
            //print_r( $stmt->fetchAll() );
            $data = $stmt->fetchAll();

            /*
            $code = $data[0]['out_code'];
            $msg = $data[0]['out_msg'];

            $result = array(
                    'code'  => (string)$code,
                    'msg'   => $msg
                );

            if($code == 1){

                $this->_db->rollBack();

                return $result;

            }
             * 
             */

            $this->_db->commit();
            
            //return $result;

        }catch (Exception $e)
        {
            $this->_db->rollBack();
            //my_error_handlier($e);
            $arrE = explode(":", $e);
            $arrE2 = explode(" in ", $arrE[1]);
            $errMsg = $arrE2[0];

            $ret = array(
                'code'  => 1,
                'msg'   => 'Kirim SMS gagal: '.$errMsg
            );
        }

        return $ret;
    }


    function transferPoint($memberId, $fromAccount, $toAccount, $jmlTransfer, $keterangan){

        $this->_db->beginTransaction();

        $result = array();

        try {

            $code = "";
            $msg = "";

            $sql = "select mobile_no from member_contact where member_id = $memberId and contact_type_id = 1";
            $resultC = $this->_db->fetchRow($sql);
            $mdn = $resultC["mobile_no"];

            $dataInput = array($fromAccount, $toAccount, $jmlTransfer, $keterangan, $mdn);
            $stmt = $this->_db->query("CALL Trx_TransferPoint(?, ?, ?, ?, ?, @out_code, @out_msg)", $dataInput);
            
            $stmt = $this->_db->query("SELECT @out_code as out_code, @out_msg as out_msg");
            //print_r( $stmt->fetchAll() );
            $data = $stmt->fetchAll();

            $code = $data[0]['out_code'];
            $msg = $data[0]['out_msg'];

            $result = array(
                    'code'  => (string)$code,
                    'msg'   => $msg
                );

            if($code == 1){

                $this->_db->rollBack();

                return $result;

            }

            $this->_db->commit();

            return $result;

        }catch (Exception $e)
        {
            $this->_db->rollBack();
            //my_error_handlier($e);
            $arrE = explode(":", $e);
            $arrE2 = explode(" in ", $arrE[1]);
            $errMsg = $arrE2[0];

            $result = array(
                'code'  => '1',
                'msg'   => $errMsg
            );

            return $result;
        }
    }

    function saveToPersonalTrans($dataTrans){
        $this->_db->beginTransaction();

        try {
            $res = $this->_db->insert('personal_data_trans',$dataTrans);
            
            if (!$res) {
                $status = array(
                    'code' => '1',
                    'msg' => 'Simpan dalam daftar pribadi gagal.'
                );

                $this->_db->rollBack();
                return $status;
            }

            $status = array(
                'code' => '0',
                'msg' => 'Transfer Sukses.'
            );

            $this->_db->commit();

            return $status;

        }catch (Exception $e) {
            $this->_db->rollBack();

            $status = array(
                'code' => '1',
                'msg' => 'Simpan dalam daftar pribadi gagal. '.$e
            );


            return $status;
        }
    }


    function getDaftarTransfer($memberId, $limit, $start){

        // Get Total
        $sql = "select count(1) as total
                from personal_data_trans where member_id = '$memberId'";

        $result = $this->_db->fetchRow($sql);
        $rows = $result['total'];

        $data = array($rows);


        $sql = "select *
                from personal_data_trans where member_id = '$memberId' order by trans_name ";

        if ($limit != ""){
            $sql .= " limit $start, $limit";
        }
        
        $result = $this->_db->fetchAll($sql);

        array_push($data, $result);


        return $data;
    }

}
